﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;
using ResortModule;

namespace MBBHotelReservation
{
    public partial class frmMaintenance : Form
    {
        UnityProcessor proc = new UnityProcessor();
        UnityScript uScript;
        Threader thd = new Threader();

        frmMonitoringManager parent;
        string dateOut = "";
        string regNum = "";

        string[] oldValues = new string[0];
        public frmMaintenance(frmMonitoringManager p)
        {
            parent = p;
            InitializeComponent();
            
            proc.ProcessError +=new ProcessHandler(proc_ProcessError);
            proc.ProcessCompleted += new ProcessHandler(proc_ProcessCompleted);
            btnCommit.Click += new EventHandler(btnCommit_Click);
            this.Load += new EventHandler(frmMaintenance_Load);
            this.FormClosed += new FormClosedEventHandler(frmMaintenance_FormClosed);
        }

        void frmMaintenance_FormClosed(object sender, FormClosedEventArgs e)
        {
            //parent.reloadMonitoring();
            parent.LoadData(DateTime.Now.ToString("yyyy-MM-dd HH:mm:00"));
        }

        void btnCommit_Click(object sender, EventArgs e)
        {
            //if(parent.isMaintain == false)
                getRecord();
           


            
        }
       /* void submitMaintenance()
        {
            UnityScript aus = new UnityScript("GETREC", 14);
            aus.Script_Clear();
            aus.Script_Add(Script.PROC_NAME, "SAVERESULT");
            aus.Script_Add(Script.DBPUX_BEGIN);
            if()
            aus.Script_Add(Script.DBPUX_INSERT, "SELECT * FROM tblMonitoring WHERE (fCheckInDateTime <= '" + search + "' AND fCheckOutDateTime >= '" + search + "')  OR (fTransactionMode = 'HouseKeeping' AND fRoom = '" + txtRoomNumber.Text + "')  OR (fTransactionMode = 'Maintenance' AND fRoom = '" + txtRoomNumber.Text + "')");
            aus.Script_Add(Script.DBPUX_COMMIT);
            proc.Run(aus);
        }*/
        void getRecord()
        {
            string search = DateTime.Now.ToString("yyyy-MM-dd HH:mm:00");
            UnityScript aus = new UnityScript("GETREC", 14);
            while (proc.IsRunning)
            { }
            aus.Script_Clear();
            aus.Script_Add(Script.PROC_NAME, "GETREC");
            aus.Script_Add(Script.DBPUX_BEGIN);
            aus.Script_Add(Script.DBPUX_QUERY, "SELECT * FROM tblMonitoring WHERE (fCheckInDateTime <= '" + search + "' AND fCheckOutDateTime >= '" + search + "' AND fRoom  = '" + txtRoomNumber.Text + "')  OR (fTransactionMode = 'HouseKeeping' AND fRoom = '" + txtRoomNumber.Text + "')  OR (fTransactionMode = 'Maintenance' AND fRoom = '" + txtRoomNumber.Text + "')");
            aus.Script_Add(Script.DBPUX_COMMIT);
            proc.Run(aus);
        }
        void frmMaintenance_Load(object sender, EventArgs e)
        {
            txtRoomNumber.Text = parent.room;
        }

        
        
        void proc_ProcessCompleted(string id, string desc)
        {
            thd.SafeCall(this, delegate
            {
                switch (id)
                {
                    case "Update":
                        MessageBox.Show("Maintenance Done...");
                        this.Close();
                        break;
                    case "GETREC":
                        UnityScript [] fus_ = new UnityScript[0];
                        proc.DBPUX_Data_Fetch(out fus_);
                        string result = "0";
                        if (fus_[0].Data_Count > 0)
                        {
                            UnityElement ue;
                            fus_[0].Data_Get(0, out ue);

                            oldValues = new string[] { ue.Data_ID.ToString(), ue.Data_Stamp };
                            dateOut = ue.Data_GetField(4).ToString();
                            regNum = ue.Data_GetField(1).ToString();
                            if (!dateOut.Equals(""))
                            {
                                result = DateTime.Now.Subtract(DateTime.Parse(dateOut)).Hours.ToString();
                            }
                        }
                        uScript = new UnityScript("Update",27);
                        
                        uScript.Script_Clear();
                        uScript.Script_Add(Script.PROC_NAME, "Update");
                        uScript.Script_Add(Script.DBPUX_BEGIN);
                        //insert method
                        string recommendation = "";
                        if (chkOK.Checked == true)
                            recommendation = "OK";
                        else
                            recommendation = "PROBLEM";

                        uScript.Script_Add(Script.DBPUX_INSERT, "tblMaintenance (fDate, fRegNum, fRoomNumber, fFindings, fMaintenanceOfficer, fRecommendation, fNumberHrsDays)",
                            new string[] { DateTime.Now.ToString("yyyy-MM-dd"), regNum, txtRoomNumber.Text, txtFindings.Text, txtPersonInCharge.Text, recommendation, result});

                        if (chkOK.Checked == true)
                        {
                            if (parent.isMaintain == true)
                            {
                                //uScript.Script_Add(Script.DBPUX_INSERT, "tblMonitoring (fRegNum, fDate, fRoom, fTransactionMode)",
                                //new string[] { DateTime.Now.ToString("yyyy-MM-dd"), txtRoomNumber.Text, "Maintenance" });

                                /*uScript.Script_Add(Script.DBPUX_UPDATE, "tblMonitoring SET " +
                                    "fTransactionMode='Cleared', " +
                                    "fCheckOutDateTime = '" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:00") + "'"
                                    ,
                                    "(fID='" + oldValues[0] + "') AND " +
                                    "(fStamp='" + oldValues[1] + "')");
                                 */
                                if (parent.isWhite == false)
                                {
                                    uScript.Script_Add(Script.DBPUX_UPDATE, "tblMonitoring SET " +
                                    "fTransactionMode='Cleared', " +
                                    "fCheckOutDateTime = '" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:00") + "'"
                                    ,
                                    "(fID='" + oldValues[0] + "') AND " +
                                    "(fStamp='" + oldValues[1] + "')");
                                }
                            }
                            else
                            {
                                uScript.Script_Add(Script.DBPUX_UPDATE, "tblMonitoring SET " +
                                    "fTransactionMode='Cleared', " +
                                    "fCheckOutDateTime = '" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:00") + "'"
                                    ,
                                    "(fID='" + oldValues[0] + "') AND " +
                                    "(fStamp='" + oldValues[1] + "')");
                            }
                           
                        }
                        else
                        {
                            if (parent.isMaintain == false)
                            {
                                uScript.Script_Add(Script.DBPUX_UPDATE, "tblMonitoring SET " +
                                    "fTransactionMode='Maintenance', " +
                                    "fCheckOutDateTime = '" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:00") + "'"
                                    ,
                                    "(fID='" + oldValues[0] + "') AND " +
                                    "(fStamp='" + oldValues[1] + "')");
                            }
                            else
                            {
                                uScript.Script_Add(Script.DBPUX_INSERT, "tblMonitoring (fDate, fRoom, fTransactionMode, fCheckInDateTime)",
                                new string[] { DateTime.Now.ToString("yyyy-MM-dd"), txtRoomNumber.Text, "Maintenance", DateTime.Now.ToString("yyyy-MM-dd hh:mm:00") });

                            }
                        }
                        
                        uScript.Script_Add(Script.DBPUX_COMMIT);
                        proc.Run(uScript);
                        
                        break;
                }
            });
        }

        void proc_ProcessError(string id, string desc)
        {
            thd.SafeCall(this, delegate
            {
                MessageBox.Show(desc);
            });
        }
        
    }
}
